package com.zsummer.zero.jdk.jvm;

public class Demo4 {

	private StringBuilder sb1 = new StringBuilder();
	private StringBuffer sb2 = new StringBuffer();
	
	public static void main(String[] args) {
		
		Demo4 demo4 = new Demo4();
		Thread thread1 = new Thread(() -> {
			synchronized (demo4.getSb1()) {
				demo4.getSb1().append("abc");
			}
			demo4.getSb2().append("abc");
		});
		Thread thread2 = new Thread(() -> {
			synchronized (demo4.getSb1()) {
				demo4.getSb1().append("def");
			}
			demo4.getSb2().append("def");
		});
		thread1.start();
		thread2.start();
		try {
			thread1.join();
			thread2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(demo4.getSb1().toString());
		System.out.println(demo4.getSb2().toString());
	}
	
	protected StringBuilder getSb1() {
		return sb1;
	}


	protected void setSb1(StringBuilder sb1) {
		this.sb1 = sb1;
	}


	protected StringBuffer getSb2() {
		return sb2;
	}


	protected void setSb2(StringBuffer sb2) {
		this.sb2 = sb2;
	}
}
